//
// Created by YueChuan on 2023/5/5.
//
#include "http_server.h"
#include "log.h"

static yuechuan::Logger::ptr g_logger = YUECHUAN_LOG_ROOT();
yuechuan::IOManager::ptr worker;

void run() {
    g_logger->setLevel(yuechuan::LogLevel::INFO);
    yuechuan::http::HttpServer::ptr server(new yuechuan::http::HttpServer());
    yuechuan::Address::ptr addr = yuechuan::Address::LookupAnyIPAddress("0.0.0.0:8020");
    while (!server->bind(addr)) {
        sleep(2);
    }
    auto dispatch = server->getServletDispatch();
    dispatch->addServlet("/yuechuan", [](yuechuan::http::HttpRequest::ptr req,
                                         yuechuan::http::HttpResponse::ptr rsp,
                                         yuechuan::http::HttpSession::ptr session) {
        rsp->setBody(req->toString());
        return 0;
    });
    dispatch->addGlobServlet("/yuechuan/*", [](yuechuan::http::HttpRequest::ptr req,
                                             yuechuan::http::HttpResponse::ptr rsp,
                                             yuechuan::http::HttpSession::ptr session) {
        rsp->setBody(req->toString());
        return 0;
    });
    server->start();
}

int main(int argc, char **argv) {
    yuechuan::IOManager iom(2);
    iom.schedule(run);

    return 0;
}
